import {
    createWorldTerrain,
    Viewer,
    Cesium3DTileset,
    Cesium3DTileFeature,
    IonResource,
    HeadingPitchRange,
    Color,
    ScreenSpaceEventHandler,
    ScreenSpaceEventType,
    defined
} from 'cesium';

export default class map {
    constructor(dom) {
        this.dom = dom;
        this.viewer = null;
        this.scene = null;

        this.initViewer();
        // this.addBimModel();
        this.add3DTilesAdjustHeight();
    }
    initViewer() {
        this.viewer = new Viewer(this.dom, {
            // terrainProvider: createWorldTerrain()
        });
        this.scene = this.viewer.scene;
    }
    addBimModel() {
        var tileset = this.scene.primitives.add(
            new Cesium3DTileset({
                url: IonResource.fromAssetId(8564)
            })
        );

        // 异步zoomTo
        tileset.readyPromise
            .then(tileset => {
                this.viewer.zoomTo(
                    tileset,
                    new HeadingPitchRange(
                        0.5,
                        -0.2,
                        tileset.boundingSphere.radius * 5.0
                    )
                );
            })
            .otherwise(error => {
                console.log(error);
            });

        tileset.tileLoad.addEventListener(tile => {
            processTileFeatures(tile, loadFeature);
        });

        // 添加要素选择
        var handler = new ScreenSpaceEventHandler(this.scene.canvas);
        var selectedFeature = null;
        var elementMap = {};

        function selectFeature(feature) {
            var element = feature.getProperty('element');
            setElementColor(element, Color.YELLOW);
            selectedFeature = feature;
        }

        function unselectFeature(feature) {
            if (!defined(feature)) {
                return;
            }
            var element = feature.getProperty('element');
            setElementColor(element, Color.WHITE);
            if (feature === selectedFeature) {
                selectedFeature = undefined;
            }
        }

        function setElementColor(element, color) {
            var featuresToColor = elementMap[element];
            var length = featuresToColor.length;
            for (var i = 0; i < length; ++i) {
                var feature = featuresToColor[i];
                feature.color = Color.clone(color, feature.color);
            }
        }

        // handler.setInputAction(movement => {
        //     var feature = this.scene.pick(movement.endPosition);

        //     unselectFeature(selectedFeature);

        //     if (feature instanceof Cesium3DTileFeature) {
        //         selectFeature(feature);
        //     }
        // }, ScreenSpaceEventType.MOUSE_MOVE);

        function loadFeature(feature) {
            var element = getElement(feature);
            var features = elementMap[element];
            if (!defined(features)) {
                features = [];
                elementMap[element] = features;
            }
            features.push(feature);

            if (hiddenElements.indexOf(element) > -1) {
                feature.show = false;
            }
        }

        function processTileFeatures(tile, callback) {
            var content = tile.content;
            var innerContents = content.innerContents;
            if (defined(innerContents)) {
                var length = innerContents.length;
                for (var i = 0; i < length; ++i) {
                    processContentFeatures(innerContents[i], callback);
                }
            } else {
                processContentFeatures(content, callback);
            }
        }
    }

    add3DTilesAdjustHeight() {
        let tileSet = new Cesium3DTileset({
            url: './static/Cesium3DTiles/Tilesets/Tileset/tileset.json'
        });
        tileSet.readyPromise
            .then(tileset => {
                this.scene.primitives.add(tileset);
                this.viewer.zoomTo(
                    tileSet,
                    new HeadingPitchRange(
                        0,
                        0,
                        tileset.boundingSphere.radius * 4.0
                    )
                );
            })
            .otherwise(error => {
                console.log(error);
            });
    }
}
